﻿using SSC.ViewModels;
using SSC.Models;
using System;
using System.Windows;

namespace SSC.Views.Windows
{
    /// <summary>
    /// UserWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserWindow : Window
    {
        public UserViewModel ViewModel { get; set; }

        public UserWindow()
        {
            InitializeComponent();
            ViewModel = new UserViewModel(); // 创建 ViewModel 实例
            this.DataContext = ViewModel;    // 设置数据上下文
        }

        // 新增按钮点击事件
        private void AddUserButton_Click(object sender, RoutedEventArgs e)
        {
            var addUserWindow = new AddUserWindow();
            if (addUserWindow.ShowDialog() == true)
            {
                var newUser = new UserModel
                {
                    UserName = addUserWindow.UserName,
                    UserPassword = addUserWindow.UserPassword,
                    UserBackup = addUserWindow.UserBackup,
                    UserFace = addUserWindow.UserFace
                };

                ViewModel.AddUser(newUser);
            }
        }

        // 删除按钮点击事件
        private void DelUserButton_Click(object sender, RoutedEventArgs e)
        {
            var selectedUser = ViewModel.SelectedUser;

            if (selectedUser == null)
            {
                MessageBox.Show("请先选择要删除的用户！");
                return;
            }

            var result = MessageBox.Show($"确认删除用户：{selectedUser.UserName}？", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result != MessageBoxResult.Yes)
                return;

            try
            {
                ViewModel.DeleteUser(selectedUser);
                MessageBox.Show("删除成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"删除失败：{ex.Message}");
            }
        }
    }
}
